{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "48ef05a4-9b0c-462b-b492-59b3ed0a8daa",
   "metadata": {},
   "source": [
    "## 初始化 SecretFlow\n",
    "\n",
    "alice 和 bob 节点都需要初始化 secretflow。首先在两个节点分别选取一个可以被对方访问的地址，**注意，端口号要选取未被占用的端口**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bb69b61-3c46-473d-bc09-63458d1d74c3",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "import secretflow as sf\n",
    "import spu\n",
    "import os\n",
    "\n",
    "network_conf = {\n",
    "    \"parties\": {\n",
    "        \"alice\": {\n",
    "            \"address\": \"alice:8000\",\n",
    "        },\n",
    "        \"bob\": {\n",
    "            \"address\": \"bob:8000\",\n",
    "        },\n",
    "    },\n",
    "}\n",
    "\n",
    "party = os.getenv(\"SELF_PARTY\", \"alice\")\n",
    "sf.shutdown()\n",
    "sf.init(\n",
    "    address=\"127.0.0.1:6379\",\n",
    "    cluster_config={**network_conf, \"self_party\": party},\n",
    "    log_to_driver=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6da7bf4-0e7a-4a92-9a89-a54aa6ab31e2",
   "metadata": {},
   "source": [
    "## 初始化 SPU\n",
    "\n",
    "1. alice 的 address 请填写可以被 bob 访通的地址，并且选择一个**未被占用的端口** ，**注意不要和 Ray 端口冲突**。\n",
    "2. alice 的 listen_addr 可以和 alice address 里的端口一样。\n",
    "3. bob 的 address 请填写可以被 alice 访通的地址，并且选择一个**未被占用的端口** ，**注意不要和 Ray 端口冲突**。\n",
    "4. bob 的 listen_addr 可以和 bob address 里的端口一样。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a3d7363-fc68-489d-afb1-3723010e5a01",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "alice, bob = sf.PYU(\"alice\"), sf.PYU(\"bob\")\n",
    "spu_conf = {\n",
    "    \"nodes\": [\n",
    "        {\n",
    "            \"party\": \"alice\",\n",
    "            \"address\": \"alice:8001\",\n",
    "            \"listen_addr\": \"alice:8001\",\n",
    "        },\n",
    "        {\n",
    "            \"party\": \"bob\",\n",
    "            \"address\": \"bob:8001\",\n",
    "            \"listen_addr\": \"bob:8001\",\n",
    "        },\n",
    "    ],\n",
    "    \"runtime_config\": {\n",
    "        \"protocol\": spu.spu_pb2.SEMI2K,\n",
    "        \"field\": spu.spu_pb2.FM128,\n",
    "        \"sigmoid_mode\": spu.spu_pb2.RuntimeConfig.SIGMOID_REAL,\n",
    "    },\n",
    "}\n",
    "spu = sf.SPU(cluster_def=spu_conf)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc12b191-abdb-43f6-86f3-06b06e13b3ba",
   "metadata": {},
   "source": [
    "## 隐私求交\n",
    "\n",
    "<span style=\"color: rgb(50, 50, 50)\">SecretFlow 提供 </span>`psi_csv`<span style=\"color: rgb(50, 50, 50)\"> 函数， </span>`psi_csv`<span style=\"color: rgb(50, 50, 50)\"> 将 csv 文件作为输入，并在求交后生成 csv 文件。默认协议为 </span>[**KKRT**](https://eprint.iacr.org/2016/799.pdf)<span style=\"color: rgb(50, 50, 50)\">。</span>\n",
    "\n",
    "```\n",
    "psi_csv(\n",
    "    key: str | List[str],\n",
    "    input_path: str,\n",
    "    output_path: str,\n",
    "    receiver: str,\n",
    "    protocol=\"KKRT_PSI_2PC\",\n",
    "    precheck_input=True,\n",
    "    sort=True,\n",
    "    broadcast_result=True,\n",
    "    bucket_size=1048576,\n",
    "    curve_type=\"CURVE_25519\",\n",
    "    preprocess_path=None,\n",
    "    ecdh_secret_key_path=None,\n",
    "    dppsi_bob_sub_sampling=0.9,\n",
    "    dppsi_epsilon=3,\n",
    "    progress_callbacks: Callable[[str, ProgressData], None] | None = None,\n",
    "    callbacks_interval_ms: int = 5000,\n",
    "    ic_mode: bool = False,\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "293ad974-0904-4955-b1fa-8649331d40dd",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "current_dir = os.getcwd()\n",
    "\n",
    "input_path = {\n",
    "    alice: f\"{current_dir}/iris_alice.csv\",\n",
    "    bob: f\"{current_dir}/iris_bob.csv\",\n",
    "}\n",
    "output_path = {\n",
    "    alice: f\"{current_dir}/iris_alice_output.csv\",\n",
    "    bob: f\"{current_dir}/iris_bob_output.csv\",\n",
    "}\n",
    "spu.psi_csv(\"uid\", input_path, output_path, \"alice\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "513d1da7",
   "metadata": {},
   "outputs": [],
   "source": [
    "sf.shutdown()"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
